#include <stdio.h>
#include <stdlib.h>
#include "optiga.h"
#include "optiga_host.h"
#include "optiga_curve.h"

int main()
{
  int i;
  uint16_t        rand = 0x0;
  optiga_session  auth_session;
  optiga_uid      chip_id;
  uint16_t        pid = 0x0005, vid = 0x0005;
  uint8_t         *byteptr;
  uint8_t         chip_odc[OPTIGA_ODC_BYTE_LEN] = {
    0x8C, 0x74, 0x73, 0xA3, 0xAA, 0xD9, 0xD3, 0xCC, 0xC9, 0x7B, 0xC4, 0x92, 0x6E, 0x0A, 0xA8, 0xBB, 
    0x21, 0x60, 0x18, 0x88, 0x10, 0x4E, 0x0A, 0xF5, 0x7A, 0xE8, 0xB0, 0xC4, 0x9E, 0x96, 0x25, 0x30, 
    0xD2, 0x81, 0x9F, 0xCD, 0x85, 0x67, 0x2A, 0x4F, 0x51, 0xA1, 0x26, 0x72, 0xB2, 0x5C, 0x59, 0x75, 
    0xF9, 0xF4, 0xC8, 0xC6, 0xA1, 0x97, 0xCC, 0x55, 0x16, 0xB5, 0x7B, 0xAD, 0x08, 0x13, 0x35, 0x65, 
    0xFA, 0x17, 0x0A, 0xF8, 0x6B, 0x2F, 0xDF, 0xCB, 0x62, 0xC4, 0x93, 0x0C, 0x21, 0x1A
  };// inital 0
  static UBYTE    response[22] = {
    0xE1, 0x3F, 0xE0, 0x58, 0xF8, 0x3F, 0xF4, 0xA0, 0x11, 0x2F, 0x33, 0x20, 0x38, 0xE0, 0x7F, 0xD8,
    0x99, 0x70, 0x86, 0xC6, 0x00, 0x00
  };//inital 0
  static UBYTE    mac_data[10] = {
    0x09, 0x66, 0xDA, 0xD0, 0x55, 0xBF, 0xD5, 0x0D, 0x74, 0xBC
  };// fill 0

  chip_id.bn[0] = 0x22;
  chip_id.bn[1] = 0x78;
  chip_id.bn[2] = 0x78;
  chip_id.bn[3] = 0x10;
  chip_id.bn[4] = 0x08;
  chip_id.bn[5] = 0x04;
  chip_id.bn[6] = 0x78;
  chip_id.bn[7] = 0x26;
  chip_id.bn[8] = 0x1A;
  chip_id.bn[9] = 0x01;
  
  /* 1. rand init */ 
  optiga_hst_rand_init(rand);  //產生32B  rand pool
  /* 2. session init with chip id */
  optiga_hst_session_init(&auth_session, &chip_id);
  /* 3. select public key for odc (certificate) verification with pid, vid (can be hardcoded 0x05, 0x05) */
  optiga_curve_init(pid, vid);
  
  
  //等 chip_odc
  
  
  /* 4. verify odc */
  if (optiga_hst_verify_odc (&auth_session, chip_odc) == FALSE) {
    printf("[ERROR] ODC Verification Failed. \n");
    return 0;
  } else {
    printf("[DEBUG] ODC Verification OK. \n");
  }
  /* 5. generate challenge */
  optiga_hst_gen_challenge (&auth_session);
  byteptr = (uint8_t *)auth_session.lambda;
  printf("lambda    = [");
  for (i = 0; i < sizeof(auth_session.lambda); i++) {
    printf("%02X ", byteptr[i]);
  }
  printf("]\n");
  byteptr = (uint8_t *)auth_session.challenge;
  printf("challenge = [");
  for (i = 0; i < sizeof(auth_session.challenge); i++) {
    printf("%02X ", byteptr[i]);
  }
  printf("]\n");
  
  
  
  
  
  
  
  /* 6. verify response */
  if (optiga_hst_verify_response (&auth_session, response, mac_data) == FALSE){
    printf("[ERROR] Response Verification Failed. \n");
    return 0;
  } else { 
    printf("[DEBUG] Response Verification OK. \n");
  }

  return 0;
}
